package com.egdigital.allzic.adswizz;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.util.Log;

/**
 * This class manage ADSWIZZ http requests <br>
 * <b>see</b> {@link GetHttpTask} <br>
 * <b>also</b> {@link HttpGet} - {@link DefaultHttpClient}
 */
public class AdsHttpRequest {

	private static final String		TAG		= AdsHttpRequest.class.getSimpleName(); // log
	private GetHttpTask				mGetAsyncTask;
	private static AdsHttpRequest	mInstance;
	private OnGetRequestListener	mCallBack;
	private static final String		SUCCESS	= "success";
	private static final String		SUCCES	= "succes";
	private static final String		FAILED	= "fail";

	/**
	 * @return a singleton instance of {@link AdsHttpRequest}
	 */
	public static AdsHttpRequest getInstance() {
		if (mInstance == null) {
			synchronized (AdsHttpRequest.class) {
				if (mInstance == null) {
					mInstance = new AdsHttpRequest();
				}
			}
		}
		return mInstance;
	}

	/**
	 * Initialize the {@link AsyncTask}, set the callback, execute the task
	 * 
	 * @param url
	 *        url for the request
	 * @param callback
	 *        {@link OnGetRequestListener} for feed back
	 */
	public void postToAdswizz(String url, OnGetRequestListener callback) {
		mCallBack = callback;
		if (mGetAsyncTask == null) {
			mGetAsyncTask = new GetHttpTask();
		} else {
			cancelGetTask();
			mGetAsyncTask = new GetHttpTask();
		}
		mGetAsyncTask.execute(url);
	}

	/**
	 * cancel the {@link AsyncTask} if it's still alive <br>
	 * <b>see </b> {@link Status}
	 */
	public void cancelGetTask() {
		if (mGetAsyncTask != null && mGetAsyncTask.getStatus().equals(Status.RUNNING)) {
			mGetAsyncTask.cancel(true);
		}
		mGetAsyncTask = null;
	}

	private AdsHttpRequest() {
		super();
	}

	/**
	 * Actually construct and launch the HTTP request
	 * 
	 * @param url
	 *        url of the request
	 * @return response of the server
	 */
	private String getResponseFromUrl(String url) {
		String xml = null;
		try {
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpGet httpGet = new HttpGet(url);
			HttpResponse httpResponse = httpClient.execute(httpGet);
			HttpEntity httpEntity = httpResponse.getEntity();
			xml = EntityUtils.toString(httpEntity);
		} catch (Exception e) {
			Log.e(TAG, "", e);
		}
		return xml;
	}

	/**
	 * Manage the http request in background
	 * 
	 * @param String
	 *        url for the request
	 */
	private class GetHttpTask extends AsyncTask<String, String, String> {

		@Override
		protected String doInBackground(String... params) {
			if (params[0] != null) {
				return getResponseFromUrl(params[0]); // return the response of the server
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			if (result != null) {
				if (result.contains(SUCCES) || result.contains(SUCCESS)) {
					mCallBack.onGetRequestResult(SUCCESS);
				} else {
					mCallBack.onGetRequestResult(FAILED);
				}
			}
		}
	}
}
